﻿using Autofac;
using Ewell.BizComponent.UI.Core.Model;
using Ewell.BizComponent.UI.Core.VievModel;
using Ewell.BizComponent.UI.Reg.ViewModel;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.GateWay.Dto.Athena;
using Ewell.Interface.Common.Model.GateWay.Dto.Dict;
using Ewell.Interface.Common.Model.GateWay.GateWayUriInfo;
using Ewell.Interface.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ewell.BizComponent.UI.Reg.Views
{
    /// <summary>
    /// OutRegView.xaml 的交互逻辑
    /// </summary>
    public partial class OutRegView : UserControl
    {
        public static readonly DependencyProperty OutRegVmProperty = DependencyProperty
            .Register("outRegVm", typeof(OutRegVm), typeof(OutRegView),
            new FrameworkPropertyMetadata(new OutRegVm()));
        public OutRegVm outRegVm
        {
            get {
                if (this.DataContext.GetType() != typeof(OutRegVm))
                    return (OutRegVm)GetValue(OutRegVmProperty);
                else
                    return this.DataContext as OutRegVm;
            }
            set { SetValue(OutRegVmProperty, value); }
        }
        public OutRegView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //if (outRegVm == null)
            //    outRegVm = Global.Container.Resolve<OutRegVm>();
            if (outRegVm == null) outRegVm = new OutRegVm();
            #region 查询按钮权限
            try
            {
                LoginRequest request = new LoginRequest()
                {
                    account = LoginInfo.loginUser.staffCode,
                };
                UserDubboService userDubboService = new UserDubboService();
                var athena = userDubboService.getAthenaList.Post<AthenaResp>(request);
                if (athena.success)
                {
                    if (athena.@object.buttons != null)
                    {
                        var buttonResp = athena.@object.buttons.FirstOrDefault(f => f.code == "OUT-REG-FREEREG");
                        if (buttonResp==null||buttonResp.isDisable == "1")
                            outRegVm.freeBtnVisib = Visibility.Collapsed;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Debug("查询按钮权限异常!" + ex.ToString());
            }
            #endregion

            #region 查询医保费别
            DictPatientTypeDubboService dictPatientTypeDubboService = new DictPatientTypeDubboService();
            var gateWayResult = dictPatientTypeDubboService.queryPagePatientType.Post<List<PatientTypeVm>>(new DictPatientTypeQueryRequest
            {
                athenaMenuId = LoginInfo.menuId,
                enabledFlag = "1",
            }).CheckSuccess("HISBIZ");
            this.outRegVm.patientTypeList = gateWayResult.@object;
            if (this.outRegVm.patient != null && this.outRegVm.selectedPatientType == null)
            {
                this.outRegVm.selectedPatientType = this.outRegVm?.patientTypeList?.FirstOrDefault(f => f.no == this.outRegVm?.patient?.patientTypeNo);
                if (this.outRegVm.selectedPatientType == null)
                    this.outRegVm.selectedPatientType = this.outRegVm?.patientTypeList?.FirstOrDefault();
            }
            #endregion

            //查询排班
            outRegVm.useDoct = true;
            outRegVm.hasSched = true;
            outRegVm.querySched.Execute(null);
            this.DataContext = outRegVm;
        }

        private void hasSchedBtn_Checked(object sender, RoutedEventArgs e)
        {
            if (this.outRegVm.hasSched)
            {
                quantityCol.Visibility = Visibility.Visible;
                quantityUsedCol.Visibility = Visibility.Visible;
                quantityRemainCol.Visibility = Visibility.Visible;
                noonTypeNameCol.Visibility = Visibility.Visible;
            }
            else
            {
                quantityCol.Visibility = Visibility.Collapsed;
                quantityUsedCol.Visibility = Visibility.Collapsed;
                quantityRemainCol.Visibility = Visibility.Collapsed;
                noonTypeNameCol.Visibility = Visibility.Collapsed;
            }
        }

        private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            (this.DataContext as OutRegVm).register.Execute(this.outRegVm.selectedSchedule);
        }

        private void useDoctBtn_Checked(object sender, RoutedEventArgs e)
        {

        }
    }
}
